#!/bin/sh -e
# Distributed under the terms of the BSD License.
# Copyright (c) 2011 Phil Cryer phil.cryer@gmail.com
# Source https://github.com/philcryer/lipsync

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

### BEGIN INIT INFO
# Provides:          lipsyncd
# Required-Start:    $syslog $local_fs
# Required-Stop:     $syslog $local_fs
# Should-Start:      dbus avahi
# Should-Stop:       dbus avahi
# Default-Start:     2 3 4 5
# Default-Stop:      1
# Short-Description: a service that provides automated, command-line syncing
### END INIT INFO

DAEMON=/usr/local/bin/lipsyncd
NAME=lipsyncd
DESC="a service that provides command-line, automated syncing between systems"
LOGLEVEL=scarce
DELAY_SEC=2
CONF_FILE=/etc/lipsyncd

test -x $DAEMON || exit 0

if [ -e $CONF_FILE ]; then
        . $CONF_FILE
fi

LOGFILE=/home/$USER_NAME/.$NAME/$NAME.log
PIDFILE=/home/$USER_NAME/.$NAME/$NAME.pid
RUN=yes
RUN_AS_USER=$USER_NAME

# Include lipsyncd defaults if available
if [ -f /etc/default/lipsyncd ] ; then
    . /etc/default/lipsyncd
fi

# ensure user's log/pid dir is avail, create if not
 if [ ! -d /home/$USER_NAME/.$NAME ]; then
                mkdir /home/$USER_NAME/.$NAME
        fi
# verify sane perms on log/pid dir
chown $USER_NAME:$USER_NAME /home/$USER_NAME/.$NAME

if [ "x$RUN" != "xyes" ] ; then
    exit 0
fi

DAEMON_OPTS="-a $RUN_AS_USER"

set -e

case "$1" in
  start)
	echo -n "Starting $NAME daemon..."
	su $RUN_AS_USER -c "$DAEMON -log $LOGLEVEL -delay $DELAY_SEC -logfile $LOGFILE -pidfile $PIDFILE -rsyncssh $LOCAL_DIR $REMOTE_HOST $REMOTE_DIR"
	echo "done"
	#echo `ps -fe | grep "/usr/local/bin/lipsyncd" | grep -v grep | cut -d' ' -f7` > $PIDFILE
	echo `pidof lipsyncd` > $PIDFILE
	#echo "====== lipsyncd (pid `cat $PIDFILE`) started on `date` =====" >> $LOGFILE
	#echo "====== lipsyncd (pid `pidof lipsyncd`) started on `date` =====" >> $LOGFILE
	echo "`date "+%a %b %d %T %Y"` Daemon: lipsyncd has started as pid `pidof lipsyncd`" >> $LOGFILE
	;;
  stop)
	echo -n "Stopping $NAME daemon..."
	#start-stop-daemon --stop --oknodo --quiet --pidfile /var/run/$NAME.pid \ --exec $DAEMON
	#echo "====== lipsyncd stopped on `date` =====" >> $LOGFILE
	echo "`date "+%a %b %d %T %Y"` Daemon: lipsyncd is stopping" >> $LOGFILE
	rm $PIDFILE
	`killall lipsyncd`
	echo "done"
	;;
  force-reload)
	# check whether $DAEMON is running. If so, restart
	start-stop-daemon --stop --test --quiet --pidfile \
		$PIDFILE --exec $DAEMON \
	&& $0 restart \
	|| exit 0
	;;
  restart)
	echo -n "Restarting $DESC: "
	start-stop-daemon --stop --quiet --pidfile \
		$PIDFILE --exec $DAEMON
	sleep 1
	start-stop-daemon --start --quiet --pidfile \
		$PIDFILE --exec $DAEMON -- $DAEMON_OPTS
	echo "$NAME."
	;;
  status)
	if [ -s $PIDFILE ]; then
        	echo "$NAME is running as PID `cat $PIDFILE`."
                exit 0
        else
           	echo "$NAME not running."
           	exit 1
        fi
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
